<?php

require_once '../util/timer.php';
require_once '../db/db.php'; 
require_once '../db/get/thread.php';
require_once '../config.php';
require_once '../util/consts.php';
require_once '../util/Bitter.php';
require_once '../util/xhtml.php';

$_TPL = array();

$stimer = mtimer();
echoThreads();
echo "foo" . mtimer( $stimer );

function echoThreads() {
  global $_TPL;

  $psize = ((int)$_REQUEST['psize'] > 0) ? intval((int)$_REQUEST['psize']) : DEFAULT_THREAD_PAGE_SIZE;
  if( $_REQUEST['fid'] > 0 ) {
    $fid = (int)intval($_REQUEST['fid']);
    $page = ($_REQUEST['page'] > 0) ? (int)intval($_REQUEST['page']) : DEFAULT_THREAD_PAGE_START;
  } elseif( $_REQUEST['tnum'] > 0 ) {
    // aye' lass slightly slower, but what can ya do, now?
    $tid = (int)intval($_REQUEST['tnum']);

    // Yes, this doesn't work. :\ Maybe change to getFidFromTid( $tid ); ?
    //  Then just place on front page
    $thread = getThread( $tid );
    $fid = $thread['fid'];
    $start = ($thread['tid'] > $inc) ? (int)(($thread['tid']-1)/$inc)*$inc : 0;
    $thread = NULL;
  } else {
    die( "BREAKAGE!" ); // BREAKAGE
  }

  $threads = getThreads( $fid, ($page-1)*$psize, $psize );
  $count = threadCount($fid);

  for( $i = count($threads)-1; $i > -1; --$i ){ 
    // we need to desmurf all the smurfs, but where? Not in template
   // $threads[$i]['datum'] = smurf($threads[$i]['datum'], $threads[$i]['smurfKey']);
  }  

  $_TPL['fid'] = $fid;
  $_TPL['ftitle'] = $threads[0]['ftitle'];
  $_TPL['threads'] = $threads;
  $_TPL['count'] = $count;
  $_TPL['page'] = $page;
  $_TPL['psize'] = $psize;

  showTpl('Thread');

}

?>
